package com.jb.workflow.app.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import net.oschina.app.AppContext;
import net.oschina.app.AppException;
import net.oschina.app.bean.Entity;
import net.oschina.app.common.StringUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JBNewsList extends Entity {
	
	public final static int CATALOG_RUNNING = 1;//正在运行实例
//	public final static int CATALOG_COMPLETE = 2;//历史实例
//	public final static int CATALOG_ALL = 3;//全部
	
	private int pageSize;
	private List<JBNews> jbNewsList = new ArrayList<JBNews>();
	
	public List<JBNews> getJBNewslist() {
		return jbNewsList;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public static JBNewsList parse(InputStream inputStream, final int catalog) throws IOException, AppException {
		
		JBNewsList list = new JBNewsList();
		JBNews instance;
		boolean completed = true;
		
		String result = StringUtils.toConvertString(inputStream);
		
		try {
			JSONArray arr = new JSONArray(result);
			if (arr != null) {
				for (int i = 0; i < arr.length(); i++) {
					JSONObject jsObj = (JSONObject) arr.get(i);
					if (jsObj != null) {
						instance = new JBNews();
						instance.setId(jsObj.getInt("id"));
						instance.setTitle(jsObj.getString("title"));
						instance.setAuthor(jsObj.getString("author"));
						instance.setCreateTime(jsObj.getString("createTime"));
						instance.setType(jsObj.getString("type"));
						instance.setContent(jsObj.getString("content"));
						list.getJBNewslist().add(instance);
					}
				}
				list.setPageSize(arr.length());
			}
			
			return list;
			
		} catch (JSONException e) {
			throw AppException.json(e);
		} finally {
			inputStream.close();
		}
		
	}
}
